let pomelo = require("pomelo");
let logger = require("pomelo-logger").getLogger(__filename);
let DateUtil = require("../../../utils/dateUtil");

let Player = function (cfg, frontendId, sessionId) {
    this.balance = cfg["balance"];
    this.account = cfg["account"];
    this.frontendId = frontendId;
    this.sessionId = sessionId;
    this.onlineFlag = 1;
    this.position = -1;
    this.readyFlag = 0;
    this.gotReadyTime = 0;
    this.countDown = 0;
    this.watchTimes = 0;
    this.isRobot = 0;   //0玩家 1机器人
};

Player.prototype.getFrontendId = function () {
    return this.frontendId;
};

Player.prototype.getSessionId = function () {
    return this.sessionId;
};

Player.prototype.push = function (route, message) {
    if (this.isRobot === 1) {
        return;
    }
    logger.info(`route: ${route}, message: ${JSON.stringify(message)}`);
    if (this.frontendId !== '' && this.sessionId !== 0) {
        pomelo.app.rpcInvoke(this.frontendId, {
            namespace: "user", service: "pushMessageRemote", method: "pushMessage",
            args: [route, message, [this.sessionId]]
        }, function (error, failed) {
        });
    } else {
        logger.error(
            "push msg player is not online. uid=" + this.account.uid +
            "route=" + route + " msg=" + JSON.stringify(message)
        );
    }
};

Player.prototype.getUid = function () {
    return this.account.uid;
};

Player.prototype.getNickname = function () {
    return this.account.nickname;
};

Player.prototype.getAvatar = function () {
    return this.account.avatar;
};

Player.prototype.setOffline = function () {
    this.onlineFlag = 0;
    this.frontendId = '';
    this.sessionId = 0;
};

Player.prototype.isOnline = function () {
    return this.onlineFlag > 0;
};

Player.prototype.refreshConnectionInfo = function (frontendId, sessionId) {
    this.frontendId = frontendId;
    this.sessionId = sessionId;
    this.onlineFlag = 1;
};

Player.prototype.clearWatchTimes = function () {
    this.watchTimes = 0;
};

Player.prototype.addWatchTimes = function () {
    this.watchTimes++;
};

Player.prototype.getWatchTimes = function () {
    return this.watchTimes;
};

Player.prototype.setReady = function () {
    this.readyFlag = 1;
    this.clearGetReadyCountDown();
    this.clearWatchTimes();
};

Player.prototype.clearReady = function () {
    this.readyFlag = 0;
};

Player.prototype.isReady = function () {
    return this.readyFlag > 0;
};

Player.prototype.getPosition = function () {
    return this.position;
};

Player.prototype.setPosition = function (position) {
    this.position = position;
};

Player.prototype.resetInTable = function () {

};

Player.prototype.toJSON = function () {

};

Player.prototype.getBalance = function () {
    return this.balance;
};

Player.prototype.changeBalance = function (amount) {
    let prevBalance = this.balance;
    this.balance = this.balance + amount;
    if (this.balance < 0) {
        logger.error("fatal error. uid=" + this.getUid() + " balance=" + prevBalance +
            " amount=" + amount);
        this.balance = 0;
    }
};

Player.prototype.startGetReadyCountDown = function (countDown) {
    this.gotReadyTime = DateUtil.nowMS();
    this.countDown = countDown;
    this.push("onPlayerStartGotReady", {
        countDown: countDown
    });
};

Player.prototype.clearGetReadyCountDown = function () {
    this.gotReadyTime = 0;
};

Player.prototype.isGotReadyTimeout = function () {
    return this.gotReadyTime > 0 && DateUtil.getIntervalSec(this.gotReadyTime) > this.countDown;
};

module.exports = Player;